unit dbcDBEngine;
// Database Comparer VCL v.3.0
// Copyright (C) 1998-2012, by Boris Loboda
// www.CleverComponents.com
{$I Comparer.INC}

// 24.03.2005 (BL)
// - First implementation (for TIBSQLExec / IBX).
interface

Uses Classes, {$IFDEF Delphi6} Variants,{$ENDIF}
     dbcTypes, DB, dbcDBOptions;

type

  TDBCAbstractConnection = Class;
  TDBCQuery = Class;
  TDBCDatabase = class;

  TDBCCustomConnection = Class(TComponent)
  protected
    fDatabaseType: TDatabaseType;
    fConnectionType: TConnectionType;
    fSupportedDatabases: TDatabaseSetType;
    fSupportedConnections: TConnectionSetType;
    fConnectionOptions: TConnectionOptions;
    fIBServerOptions: TIBEngineOptions;
    fMSSQLServerOptions: TMSSQLServerOptions;
    fMySQLServerOptions: TMySQLServerOptions;
    fASAServerOptions: TASAServerOptions;
    fASEServerOptions: TASEServerOptions;
    fOracleServerOptions: TOracleServerOptions;
    fPGSQLServerOptions: TPGSQLServerOptions;
    fDB2ServerOptions: TDB2ServerOptions;
    fBeforeConnect: TNotifyEvent;
    fAfterConnect: TNotifyEvent;
    fBeforeDisconnect: TNotifyEvent;
    fAfterDisconnect: TNotifyEvent;
    fErrorMessage: TErrorMessageEvent;
    fLogNextLine: TLogNextLineEvent;
    procedure SetDatabaseType(aType: TDatabaseType); virtual;
    procedure SetConnectionType(aType: TConnectionType); virtual;
    function GetSupportedDatabases(): TDatabaseSetType; virtual;
    function GetSupportedConnections(): TconnectionSetType; virtual;
    function GetConnected(): Boolean; virtual;
    procedure SetConnected(aConnected: Boolean); virtual;
    procedure DoBeforeConnect(); virtual;
    procedure DoBeforeDisconnect(); virtual;
    procedure DoAfterConnect(); virtual;
    procedure DoAfterDisconnect(); virtual;
    procedure SetConnectionOptions(aOptions: TConnectionOptions);
    procedure SetIBServerOptions(aOptions: TIBEngineOptions);
    procedure SetMSSQLServerOptions(aOptions: TMSSQLServerOptions);
    procedure SetMySQLServerOptions(aOptions: TMySQLServerOptions);
    procedure SetASAServerOptions(aOptions: TASAServerOptions);
    procedure SetASEServerOptions(aOptions: TASEServerOptions);
    procedure SetOracleServerOptions(aOptions: TOracleServerOptions);
    procedure SetPGSQLServerOptions(aOptions: TPGSQLServerOptions);
    procedure SetDB2ServerOptions(aOptions: TDB2ServerOptions);
    procedure AssignTo(Dest: TPersistent); override;
    function GetDBConnectionString(): String;
  public
    constructor Create(aOwner: TComponent); override;
    destructor Destroy; override;
    procedure Assign(aSource: TPersistent); override;
    function CreateQuery(aOwner: TComponent): TDBCQuery; virtual;
    procedure StartTransaction; virtual;
    procedure Commit; virtual;
    procedure Rollback; virtual;
    function InTransaction: Boolean; virtual;
    function CreateDatabase(): Boolean; virtual;
    procedure ReConnect(aCharSet: String); virtual;
    procedure DoErrorMessage(aText: string); virtual;
    function TestConnectInfo(): String; virtual;
    procedure DoLogNextLine(aTxt: String);
    property ConnectionOptions: TConnectionOptions read fConnectionOptions write SetConnectionOptions;
    property IBServerOptions: TIBEngineOptions read fIBServerOptions write SetIBServerOptions;
    property MSSQLServerOptions: TMSSQLServerOptions read fMSSQLServerOptions write SetMSSQLServerOptions;
    property MySQLServerOptions: TMySQLServerOptions read fMySQLServerOptions write SetMySQLServerOptions;
    property ASAServerOptions: TASAServerOptions read fASAServerOptions write SetASAServerOptions;
    property ASEServerOptions: TASEServerOptions read fASEServerOptions write SetASEServerOptions;
    property OracleServerOptions: TOracleServerOptions read fOracleServerOptions write SetOracleServerOptions;
    property PGSQLServerOptions: TPGSQLServerOptions read fPGSQLServerOptions write SetPGSQLServerOptions;
    property DB2ServerOptions: TDB2ServerOptions read fDB2ServerOptions write SetDB2ServerOptions;
    property Connected: Boolean read GetConnected write SetConnected;
    property ConnectionType: TConnectionType read fConnectionType write SetConnectionType default cnAbstract;
    property DatabaseType: TDatabaseType read fDatabaseType write SetDatabaseType default dbAbstract;
    property SupportedDatabases: TDatabaseSetType read GetSupportedDatabases;
    property SupportedConnections: TConnectionSetType read GetSupportedConnections;
    property DBConnectionString: String read GetDBConnectionString;
    property OnBeforeConnect: TNotifyEvent read fBeforeConnect write fBeforeConnect;
    property OnAfterConnect: TNotifyEvent read fAfterConnect write fAfterConnect;
    property OnBeforeDisconnect: TNotifyEvent read fBeforeDisConnect write fBeforeDisConnect;
    property OnAfterDisconnect: TNotifyEvent read fAfterDisConnect write fAfterDisConnect;
    property OnErrorMessage: TErrorMessageEvent read fErrorMessage write fErrorMessage;
    property OnLogNextLine: TLogNextLineEvent read fLogNextLine write fLogNextLine;
  end;

  // Uni-connection
  TDBCConnection = Class(TDBCCustomConnection)
  protected
    fConnection: TDBCAbstractConnection;
    fDatabase: TDBCDatabase;
    function FindConnection(aType: TConnectionType): TDBCAbstractConnection;
    procedure SetConnectionType(aType: TConnectionType); override;
    function GetSupportedDatabases(): TDatabaseSetType; override;
    function GetSupportedConnections(): TConnectionSetType; override;
    function GetConnected(): Boolean; override;
    procedure SetConnected(aConnected: Boolean); override;
    function UnitName(aType: TConnectionType): string;
    procedure SetDatabaseType(aType: TDatabaseType); override;
  public
    constructor Create(aOwner: TComponent); override;
    destructor Destroy; override;
    function CreateQuery(aOwner: TComponent): TDBCQuery; override;
    procedure StartTransaction; override;
    procedure Commit; override;
    procedure Rollback; override;
    function InTransaction: Boolean; override;
    function CreateDatabase(): Boolean; override;
    procedure ReConnect(aCharSet: String); override;
    property Connection: TDBCAbstractConnection read fConnection;
    property DBCDatabase: TDBCDatabase read fDatabase;
  published
    property SupportedDatabases: TDatabaseSetType read GetSupportedDatabases stored False;
    property SupportedConnections: TConnectionSetType read GetSupportedConnections stored False;
    property ConnectionOptions;
    property IBServerOptions;
    property MSSQLServerOptions;
    property MySQLServerOptions;
    property ASAServerOptions;
    property ASEServerOptions;
    property OracleServerOptions;
    property PGSQLServerOptions;
    property DB2ServerOptions;
    property DatabaseType;
    property ConnectionType;
    property OnErrorMessage;
    property OnLogNextLine;
    property OnBeforeConnect;
    property OnAfterConnect;
    property OnBeforeDisconnect;
    property OnAfterDisconnect;
  end;

  TDBCDatabase = class(TComponent)
  protected
    fConnection: TDBCCustomConnection;
    function GetConnected(): Boolean; virtual;
    procedure SetConnected(aConnected: Boolean); virtual;
  public
    constructor Create(aOwner: TComponent); override;
    destructor Destroy; override;
    property Connected: Boolean read GetConnected write SetConnected;
    procedure StartTransaction; virtual;
    procedure Commit; virtual;
    procedure Rollback; virtual;
    function InTransaction(): Boolean; virtual;
    procedure ReConnect(aCharSet: String); virtual;
  end;

  TDBCQuery = class(TDataSet)
  protected
    fConnection: TDBCCustomConnection;
    fDatabase: TDBCDatabase;
    function GetSQL(): TStrings; virtual;
    function GetEof(): Boolean; virtual;
    procedure SetParamCheck(aCheck: Boolean); virtual;
    function GetParamCheck(): Boolean; virtual;
    function GetParams(): TParams; virtual;
    procedure SetActive(Value: Boolean); override;
    function GetQueryActive: Boolean; virtual;
    // not implemented
{$IFDEF DELPHI2009}
    function GetRecord(Buffer: TRecordBuffer; GetMode: TGetMode; DoCheck: Boolean): TGetResult; override;
{$ELSE}
    function GetRecord(Buffer: PChar; GetMode: TGetMode; DoCheck: Boolean): TGetResult; override;
{$ENDIF}
    procedure InternalClose; override;
    procedure InternalHandleException; override;
    procedure InternalInitFieldDefs; override;
    procedure InternalOpen; override;
    function IsCursorOpen: Boolean; override;
    function GetFields(): TFields; virtual;
    procedure SetDatabase(aDatabase: TDBCDatabase); virtual;
    {$IFNDEF DELPHI6}
    function AllocRecordBuffer: PChar; override;
    procedure FreeRecordBuffer(var Buffer: PChar); override;
    procedure GetBookmarkData(Buffer: PChar; Data: Pointer); override;
    function GetBookmarkFlag(Buffer: PChar): TBookmarkFlag; override;
    function GetRecordSize: Word; override;
    procedure InternalAddRecord(Buffer: Pointer; Append: Boolean); override;
    procedure InternalDelete; override;
    procedure InternalFirst; override;
    procedure InternalGotoBookmark(Bookmark: Pointer); override;
    procedure InternalInitRecord(Buffer: PChar); override;
    procedure InternalLast; override;
    procedure InternalPost; override;
    procedure InternalSetToRecord(Buffer: PChar); override;
    procedure SetBookmarkFlag(Buffer: PChar; Value: TBookmarkFlag); override;
    procedure SetBookmarkData(Buffer: PChar; Data: Pointer); override;
    procedure SetFieldData(Field: TField; Buffer: Pointer); overload; override;
    {$ENDIF}
  public
    property Connection: TDBCCustomConnection read fConnection write fConnection;
    property Database: TDBCDatabase read fDatabase write SetDatabase;
    property SQL: TStrings read GetSQL;
    property ParamCheck: Boolean read GetParamCheck write SetParamCheck;
    property Params: TParams read GetParams;
    property Eof: Boolean read GetEof;
    procedure First; virtual;
    procedure Last; virtual;
    procedure Next; virtual;
    function FieldByName(const FieldName: String): TField; virtual;
    procedure Open; virtual;
    procedure Close; virtual;
    procedure ExecSQL; virtual;
    function ParamByName(const ParamName: String): TParam; virtual;
    procedure Prepare(); virtual;
    property Fields: TFields read GetFields;
    property Active: Boolean read GetQueryActive write SetActive;
  end;

  // Abstract connection
  TDBCAbstractConnection = Class(TComponent)
  protected
    fDatabaseType: TDatabaseType;
    fConnectionType: TConnectionType;
    fSupportedDatabases: TDatabaseSetType;
    fOwnerConnection: TDBCConnection;
    procedure SetDatabaseType(aType: TDatabaseType);
    procedure SetConnectionType(aType: TConnectionType);
    function GetConnected(): Boolean; virtual;
    procedure SetConnected(aConnected: Boolean); virtual;
    function CreateDatabase(aOwner: TComponent): TDBCDatabase; virtual;
    function Create_Database(): Boolean; virtual;
    procedure DoErrorMessage(aText: String); virtual;
  public
    constructor Create(aOwner: TComponent); override;
    destructor Destroy; override;
    property DatabaseType: TDatabaseType read fDatabaseType write setDatabaseType;
    property ConnectionType: TConnectionType read fConnectionType write SetConnectionType;
    property SupportedDatabases: TDatabaseSetType read fSupportedDatabases;
    property OwnerConnection: TDBCConnection read fOwnerConnection;
    property Connected: Boolean read GetConnected write SetConnected;
    function CreateQuery(aOwner: TComponent): TDBCQuery; virtual;
    procedure ReConnect(aCharSet: String); virtual;
    procedure StartTransaction; virtual;
    procedure Commit; virtual;
    procedure Rollback; virtual;
    function InTransaction: Boolean; virtual;
  end;

function RegisterConnection(const cName: String; aConnection: TDBCAbstractConnection): Boolean;

function UnRegisterConnection(const cName: String; FreeConnection: Boolean = True): Boolean; overload;
function UnRegisterConnection(const aType: TConnectionType; FreeConnection: Boolean = True): Boolean; overload;

function ParamsParseSQL(const aParams: TParams; SQL: String; DoCreate: Boolean): String;
function Params_ParseSQL(aParams: TParams; SQL: String; DoCreate: Boolean): String;

function IsSelectSQL(aSQLText: string): Boolean;

function DefConnectionByDatabase(aDatabaseType: TDatabaseType): TConnectionType;

var
  IBConnectionType: TConnectionType;

implementation

//The full source code is available on purchasing the product

end.
